package cn.miaoyao.miaoyaoms.service.impl;

import cn.miaoyao.miaoyaoms.entity.Miaosample;
import cn.miaoyao.miaoyaoms.service.FileService;
import cn.miaoyao.miaoyaoms.service.MiaosampleService;
import cn.miaoyao.miaoyaoms.utils.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

/**
 * @Author: shushu
 * @Date: 2023/05/17/21:30
 * @Description:
 */
@Service
@Slf4j
public class FileServiceImpl implements FileService {
    @Autowired
    private ExcelUtil excelUtil;
    @Autowired
    private MiaosampleService miaosampleService;
    @Override
    @Transactional//开启事务操作。
    public boolean importExcel(MultipartFile multipartFile,MiaosampleService miaosampleService) {
        try{
            if(ExcelUtil.checkExcelExtension(multipartFile)){
                InputStream inputStream = multipartFile.getInputStream();//获取文件io流。
                Boolean aBoolean = excelUtil.simpleExcelRead(inputStream, Miaosample.class, miaosampleService);
                return aBoolean;//当对excel文件处理完之后就直接就返回为true。
            }
        }catch (IOException exception){
            exception.printStackTrace();
            return false;
        }
        return false;
    }
}
